package com.small.common.other;

import com.small.common.base.BaseConstants;
import com.small.common.exception.BaseException;
import com.small.common.msg.Msg;
import com.small.common.msg.MsgCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017/10/15 09:11
 * 描述: controller统一异常处理
 */
@ResponseBody
@ControllerAdvice("com.small")
public class GlobalExceptionConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionConfig.class);

    @ExceptionHandler(BaseException.class)
    public Msg baseExceptionHandler(BaseException ex) {
        LOGGER.error("throw BaseException-" + ex.getMessage(), ex);
        return new Msg(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Msg otherExceptionHandler(Exception ex) {
        LOGGER.error("throw Exception-" + ex.getMessage(), ex);
        return new Msg(BaseConstants.EX_OTHER_CODE, ex.getMessage());
    }

    /*
     * 温馨提示-参数不合格
     * 对象字段参数校验-字段参数校验
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public Msg fieldError(MethodArgumentNotValidException manve) {
        StringBuilder builder = new StringBuilder();
        for (FieldError fieldError : manve.getBindingResult().getFieldErrors()) {
            builder.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append("|");
        }
        int len = builder.length();
        if (len >= 1) {
            builder.deleteCharAt(len - 1);
        }
        return new Msg(builder.toString());
    }

    @ExceptionHandler({MethodArgumentTypeMismatchException.class})
    public Msg fieldError(MethodArgumentTypeMismatchException matm) {
        return new Msg("参数类型异常:" + matm.getName());
    }

    /**
     * controller单个参数校验
     */
    @ExceptionHandler({ConstraintViolationException.class})
    public Msg igCVE(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> set = e.getConstraintViolations();
        StringBuilder builder = new StringBuilder();
        for (ConstraintViolation<?> csv : set) {
            builder.append(csv.getPropertyPath()).append(":").append(csv.getMessage()).append("|");
        }
        int l = builder.length();
        if (l >= 1) {
            builder.deleteCharAt(l - 1);
        }
        return new Msg(builder.toString());
    }

    /*
     * 温馨提示-json转换异常
     */
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public Msg proessError(HttpMessageNotReadableException e) {
        return MsgCommon.TIP_10005;
    }

    /*
     * 温馨提示-请求的方式与接口定义的不一致[get|post]
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public Msg proessError() {
        return MsgCommon.TIP_10006;
    }

}
